package com.itheima.JDBC.demo;

import com.mysql.jdbc.Driver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

//案例: JDBC的入门案例.
public class ClassDemo01 {
    public static void main(String[] args) throws Exception {
        //1. 导入驱动.
        //1.1 在项目下新建 lib文件夹.
        //1.2 把mysql驱动(注意: 后缀名是.jar), 复制到该文件夹下.
        //1.3 把jar包配置到 classpath环境变量中.

        //2. 注册驱动.
        DriverManager.registerDriver(new Driver());
        //3. 获取连接对象.
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/web03", "root", "123");
        //4. 根据连接对象, 获取可以执行SQL语句的对象.
        Statement stat = conn.createStatement();
        //5. 执行SQL语句, 获取结果集.
        String sql = "select * from users;";
        ResultSet rs = stat.executeQuery(sql);
        //6. 操作结果集.
        //你把ResultSet理解为: 集合, 即可.
        while (rs.next()) {
            int uid = rs.getInt("uid");
            String username = rs.getString("username");
            String password = rs.getString("password");
            System.out.println(uid + "..." + username + "..." + password);
        }
        //7. 释放资源.
        rs.close();
        stat.close();
        conn.close();
    }
}
